package utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;



import env.City;
import env.Road;
import env.World;

public class Parser {
	private static Vector<Road> roads;
	private static Vector<City> cities;

	private static void parseV(String line) {
		int citiesNumber = Integer.parseInt(line.substring(2));
		// cities.setSize(citiesNumber);
		for (Integer i = 1; i <= citiesNumber; i++)
			cities.add(new City(i.toString(), citiesNumber));
	}

	public static World parseWorldFile(String filename) {
		roads = new Vector<Road>();
		cities = new Vector<City>();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(filename));
			// StringBuilder sb = new StringBuilder();
			String line = br.readLine();

			while (line != null) {
				// sb.append(line);
				// sb.append('\n');
				System.out.println(line);
				parseLine(line);

				line = br.readLine();

			}
			br.close();
			// String everything = sb.toString();
		}

		catch (FileNotFoundException e) {
			//  Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			//  Auto-generated catch block
			e.printStackTrace();
		}

		return new World(roads, cities);

	}

	private static void parseC(String line) {
		String[] tokens = line.substring(2).split("[ ]+");
		for (int i = 0; i < cities.size(); i++) {
			if (tokens[0].equals(cities.elementAt(i).getName()))
				cities.elementAt(i).setChems(Integer.parseInt(tokens[1]));
		}
	}

	public static void parseLine(String line) {

		if (line.length() > 0) {
			switch (line.charAt(0)) {
			case 'V':
				parseV(line);
				break;
			case 'E':
				parseE(line);
				break;
			case 'C':
				parseC(line);
				break;
			case 'M':
				parseM(line);
				break;
			case 'A':
				parseA(line);
				break;
			default:
				System.out.println("ERROR: can't parse:" + line);
				break;

			}
		}

	}

	private static void parseA(String line) {
		String[] tokens = line.substring(2).split("[ ]+");
		;
		for (int i = 0; i < cities.size(); i++)

		{
			if (tokens[0].equals(cities.elementAt(i).getName()))
				//cities.elementAt(i).addAgent(new Human(null, cities.elementAt(i)));
				cities.elementAt(i).addAgent(null);
		}

	}

	private static void parseM(String line) {
		String[] tokens = line.substring(2).split("[ ]+");
		for (int i = 0; i < cities.size(); i++) {
			if (tokens[0].equals(cities.elementAt(i).getName()))
				cities.elementAt(i).addEscort(Integer.parseInt(tokens[1]));
		}

	}

	private static void parseE(String line) {
		String[] tokens = line.substring(2).split("[ ]+");

		City e1 = null;
		City e2 = null;
		for (int i = 0; i < cities.size(); i++) {
			if (tokens[0].equals(cities.elementAt(i).getName()))
				e1 = cities.elementAt(i);
			if (tokens[1].equals(cities.elementAt(i).getName()))
				e2 = cities.elementAt(i);
		}
		if (e1 != null && e2 != null) {
			if (tokens[2].charAt(0) < '0' || tokens[2].charAt(0) > '9')
				tokens[2]=tokens[2].substring(1);
			Road road = new Road(e1, e2, Integer.parseInt(tokens[2]),
					tokens[3].equals("B"));
			roads.add(road);
			e1.addRoad(road);
			e2.addRoad(road);
		} else
			System.out
					.println("ERROR: Tried to build road to non-existent city");

	}
}
